package bavaria.hightech.testit.pruefungen.ss09;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class SwingClient {
	
	private JFrame frame = new JFrame("Wort raten");
	private JTextField outTextField = new JTextField(25);
	private JTextField inTextField = new JTextField();
	private JLabel msgLabel = new JLabel("Ihr Punktestand:                ");
	private GuessWord guessWord;
	private Timer timer = new Timer();
	private String player;
	
	public SwingClient(final String player, final GuessWord guessWord){
		this.player = player;
		this.guessWord = guessWord;
		frame.setLayout(new GridLayout(0, 1));
		outTextField.setEditable(false);
		outTextField.setFont(new Font("Courier", Font.PLAIN, 16));
		frame.add(outTextField);
		inTextField.setFont(new Font("Courier", Font.PLAIN, 16));
		inTextField.addKeyListener(new InputListener());
		frame.add(inTextField);
		frame.add(msgLabel);
		frame.setVisible(true);
		frame.pack();
		timer.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				outTextField.setText(guessWord.getWordMask());
				msgLabel.setText("Ihr Spielstand " +player +": " +guessWord.getScore(player));
				
			}
		}, 0, 1000);
	}
	
	private class  InputListener implements KeyListener{


		@Override
		public void keyTyped(KeyEvent e) {
			
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			if(key == KeyEvent.VK_ENTER){
				if(guessWord.guess(player, inTextField.getText())){
					JOptionPane.showMessageDialog(frame, "Gratulation, Sie haben " +guessWord.getEarnedPoints() +" Punkte erreicht.");
				}
			}
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
